#region References
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Collections.ObjectModel;

using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.Skins;

using Autumoon.CodeLibrary.BusinessLogic;
using Autumoon.CodeLibrary.CommonFoundation;
#endregion

namespace Autumoon.CodeLibrary.Forms
{
    public partial class MainRibbonForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        // Properties and fields.
        #region Properties and fields
        private static MainRibbonForm _instance = null;
        public static MainRibbonForm Instance
        {
            get { return ((_instance == null || _instance.IsDisposed) ? _instance = new MainRibbonForm() : _instance); }
        }

        private bool _isModified = false;
        public bool IsModified
        {
            get { return this._isModified; }
            set { this._isModified = value; }
        }

        private string _languageColumnName = "Language";
        #endregion

        // Constructors.
        #region Constructors
        /// <summary>
        /// Required designer variable.
        /// </summary>
        public MainRibbonForm()
        {
            // Required for Windows Form Designer support
            InitializeComponent();

            SkinManager.EnableFormSkins();

            try
            {
                this.SetTheme(OptionManager.GetCurrentTheme());
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source);
            }
        }
        #endregion

        // Public methods.
        #region Public Methods
        #endregion

        // Protected methods.
        #region Protected Methods
        #endregion

        // Private methods.
        #region Private Methods
        /// <summary>
        /// Bind the language data.
        /// </summary>
        private void LanguageDataBinding()
        {
            this.directoryTreeView.Nodes.Clear();
            this.languageComboBoxEdit.Properties.Items.Clear();

            try
            {
                DataSet languages = LanguageManager.GetAllLanguages();
                string language = String.Empty;

                if (languages != null && languages.Tables.Count > 0)
                {
                    foreach (DataRow row in languages.Tables[0].Rows)
                    {
                        language = row[this._languageColumnName].ToString();

                        this.languageComboBoxEdit.Properties.Items.Add(new ComboBoxItem(language));
                        this.directoryTreeView.Nodes.Add(language);
                    }
                }

                if (this.directoryTreeView.Nodes.Count > 0)
                {
                    this.CodeSnippetDataBinding();
                    this.SetTreeViewImage();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source);
            }
        }

        /// <summary>
        /// Bind the code snippet data.
        /// </summary>
        private void CodeSnippetDataBinding()
        {
            if (this.directoryTreeView.Nodes.Count == 0)
            {
                return;
            }

            Collection<CodeSnippetProperty> codeSnippets = null;

            foreach (TreeNode node in this.directoryTreeView.Nodes)
            {
                node.Nodes.Clear();

                codeSnippets = CodeSnippetManager.GetCodeSnippetArrayList(node.Text);

                if (codeSnippets == null || codeSnippets.Count == 0)
                {
                    continue;
                }

                foreach (CodeSnippetProperty codeSnippet in codeSnippets)
                {
                    node.Nodes.Add(codeSnippet.Name);
                }
            }
        }

        /// <summary>
        /// Set the images of directoryTreeView's nodes.
        /// </summary>
        private void SetTreeViewImage()
        {
            ImageList imageList = new ImageList();

            imageList.Images.Add("ClosedFolder", (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.ClosedFolder);
            imageList.Images.Add("OpenFolder", (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.OpenFolder);
            imageList.Images.Add("File", (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.File);

            this.directoryTreeView.ImageList = imageList;

            foreach (TreeNode node in this.directoryTreeView.Nodes)
            {
                if (node.IsExpanded)
                {
                    node.ImageKey = node.StateImageKey = node.SelectedImageKey = "OpenFolder";
                }
                else
                {
                    node.ImageKey = node.StateImageKey = node.SelectedImageKey = "ClosedFolder";
                }

                if (node.Nodes.Count > 0)
                {
                    foreach (TreeNode treeNode in node.Nodes)
                    {
                        treeNode.ImageKey = treeNode.StateImageKey = treeNode.SelectedImageKey = "File";
                    }
                }
            }
        }

        /// <summary>
        /// Create a new code snippet.
        /// </summary>
        private void CreateNewCodeSnippet()
        {
            this.ClearAll();
        }

        /// <summary>
        /// Present a code snippet.
        /// </summary>
        /// <param name="codeSnippet">The code snippet.</param>
        private void PresentCodeSnippet(CodeSnippetProperty codeSnippet)
        {
            if (codeSnippet == null)
            {
                return;
            }

            this.nameTextEdit.Text = codeSnippet.Name;
            this.nameTextEdit.Properties.ReadOnly = true;
            this.functionTextEdit.Text = codeSnippet.Function;
            this.languageComboBoxEdit.SelectedIndex = this.languageComboBoxEdit.Properties.Items.IndexOf(codeSnippet.Language);
            this.summaryMemoEdit.Text = codeSnippet.Summary;
            this.codeMemoEdit.Text = codeSnippet.Code;

            this.IsModified = false;
        }

        /// <summary>
        /// Validate the code snippet data.
        /// </summary>
        /// <returns>Is the data correct or wrong.</returns>
        private bool ValidateData()
        {
            if (String.IsNullOrEmpty(this.nameTextEdit.Text))
            {
                throw new ArgumentNullException("Name can not be null.");
            }

            if (String.IsNullOrEmpty(this.functionTextEdit.Text))
            {
                throw new ArgumentNullException("Function can not be null.");
            }

            if (this.functionTextEdit.Text.Length > 200)
            {
                throw new ArgumentException("Function content can not be more than 200 chars.");
            }

            if (this.languageComboBoxEdit.SelectedIndex == -1 && String.IsNullOrEmpty(this.languageComboBoxEdit.Text))
            {
                throw new ArgumentNullException("Language can not be null.");
            }

            if (String.IsNullOrEmpty(this.codeMemoEdit.Text))
            {
                throw new ArgumentNullException("Code can not be null.");
            }

            return true;
        }

        /// <summary>
        /// Save current code snippet.
        /// </summary>
        /// <returns>Operation succeed or failed.</returns>
        private bool SaveCodeSnippet()
        {
            try
            {
                if (this.ValidateData())
                {
                    if (!LanguageManager.CheckLanguageExists(this.languageComboBoxEdit.Text))
                    {
                        if (XtraMessageBox.Show("Do you want to add this new language type?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
                        {
                            XtraMessageBox.Show("This code snippet has not been saved yet.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                            return false;
                        }

                        LanguageManager.Insert(this.languageComboBoxEdit.Text);
                    }

                    CodeSnippetProperty codeSnippet = new CodeSnippetProperty(this.nameTextEdit.Text.Trim(), this.functionTextEdit.Text.Trim(), this.languageComboBoxEdit.SelectedItem.ToString(), this.summaryMemoEdit.Text.Trim(), this.codeMemoEdit.Text);

                    if (!CodeSnippetManager.CheckNameExists(this.nameTextEdit.Text))
                    {
                        CodeSnippetManager.Insert(codeSnippet);
                    }
                    else
                    {
                        if (XtraMessageBox.Show("The code snippet is already exist, do you want to update it?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        {
                            CodeSnippetManager.Update(codeSnippet);
                        }
                    }

                    this.nameTextEdit.Properties.ReadOnly = true;

                    return true;
                }

                return false;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Clear all editable controls' content.
        /// </summary>
        private void ClearAll()
        {
            this.nameTextEdit.Text = this.functionTextEdit.Text = this.summaryMemoEdit.Text = this.codeMemoEdit.Text = String.Empty;
            this.languageComboBoxEdit.SelectedIndex = -1;
            this.directoryTreeView.SelectedNode = null;
            this.IsModified = this.nameTextEdit.Properties.ReadOnly = false;

            this.nameTextEdit.Focus();
        }

        /// <summary>
        /// Bind the theme data.
        /// </summary>
        private void ThemeDataBinding()
        {
            this.themesRibbonGalleryBarItem.Gallery.Groups.Clear();
            this.themesRibbonGalleryBarItem.Gallery.Groups.Add(new GalleryItemGroup());

            this.themesRibbonGalleryBarItem.Gallery.ImageSize = new Size(35, 10);
            this.themesRibbonGalleryBarItem.Gallery.HoverImageSize = new Size(70, 20);
            this.themesRibbonGalleryBarItem.Gallery.AllowHoverImages = true;
            this.themesRibbonGalleryBarItem.Gallery.FixedHoverImageSize = true;

            GalleryItem item = new GalleryItem();

            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.CaramelThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Caramel;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Caramel_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.MoneyTwinsThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.MoneyTwins;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.MoneyTwins_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.BlackThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Office2007Black;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Office2007Black_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.BlueThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Office2007Blue;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Office2007Blue_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.TheAsphaltWorldThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.TheAsphaltWorld;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.TheAsphaltWorld_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.LilianThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Lilian;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.Lilian_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);

            item = new GalleryItem();
            item.Caption = item.Hint = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.iMaginaryThemeName;
            item.Image = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.iMaginary;
            item.HoverImage = (Image)global::Autumoon.CodeLibrary.Forms.Properties.Resources.iMaginary_Big;

            this.themesRibbonGalleryBarItem.Gallery.Groups[0].Items.Add(item);
        }

        /// <summary>
        /// Set the edit buttons' Enabled property.
        /// </summary>
        /// <param name="isEnable">Is enabled or not.</param>
        private void SetEditButtonsEnabled(bool isEnabled)
        {
            this.pasteBarButtonItem.Enabled
                = this.copyBarButtonItem.Enabled
                = this.cutBarButtonItem.Enabled
                = this.selectAllBarButtonItem.Enabled
                = this.undoBarButtonItem.Enabled
                = isEnabled;
        }

        /// <summary>
        /// Set the delete button's Enabled property.
        /// </summary>
        private void SetDeleteButtonEnabled(bool enabled)
        {
            this.deleteBarButtonItem.Enabled = enabled;
        }

        /// <summary>
        /// Change to English edition.
        /// </summary>
        private void ChangeToEnglish()
        {
            this.homeRibbonPage.Text = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.homeRibbonPageText;
            this.fileRibbonPageGroup.Text = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.fileRibbonPageGroupText;
            this.newBarButtonItem.Caption = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.newCaption;
            this.deleteBarButtonItem.Caption = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.deleteBarButtonItemCaption;
            this.closeBarButtonItem.Caption = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.closeBarButtonItemCaption;
            this.saveBarButtonItem.Caption = global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.saveBarButtonItemCaption;
        }

        /// <summary>
        /// Set the theme of this software.
        /// </summary>
        /// <param name="themeName">The theme's name.</param>
        public void SetTheme(string themeName)
        {
            if (String.IsNullOrEmpty(themeName) || themeName == this.defaultLookAndFeel.LookAndFeel.ActiveSkinName)
            {
                return;
            }

            this.defaultLookAndFeel.LookAndFeel.SkinName = themeName;

            try
            {
                OptionManager.SaveTheme(themeName);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        #endregion

        // Delegates and events.
        #region Delegates and Events
        private void MainRibbonForm_Load(object sender, EventArgs e)
        {
            this.LanguageDataBinding();
            this.ThemeDataBinding();
            this.SetEditButtonsEnabled(false);
            this.SetDeleteButtonEnabled(false);
        }

        private void directoryTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Node.Level == 1)
            {
                try
                {
                    this.PresentCodeSnippet(CodeSnippetManager.Query(e.Node.Text));
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, ex.Source);
                }
            }
        }

        private void directoryTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            e.Node.Checked = !e.Node.Checked;
        }

        private void newBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            bool allowCreateNew = true;

            if (this.IsModified)
            {
                DialogResult result = XtraMessageBox.Show("Do you want to save changes before creating new one?", "Notice", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Yes)
                {
                    this.SaveCodeSnippet();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }

            if (allowCreateNew)
            {
                this.CreateNewCodeSnippet();
            }
        }

        private void closeBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.ClearAll();
        }

        private void deleteBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Collection<string> languages = new Collection<string>();
            Collection<string> codeSnippetNames = new Collection<string>();

            try
            {
                foreach (TreeNode node in this.directoryTreeView.Nodes)
                {
                    if (node.Checked)
                    {
                        languages.Add(node.Text);
                    }

                    if (node.Nodes != null && node.Nodes.Count > 0)
                    {
                        foreach (TreeNode childNode in node.Nodes)
                        {
                            if (childNode.Checked)
                            {
                                codeSnippetNames.Add(childNode.Text);
                            }
                        }
                    }
                }

                if ((codeSnippetNames.Count > 0 || languages.Count > 0) && XtraMessageBox.Show("Are you sure to delete the selected code snippet(s)?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    foreach (string codeSnippetName in codeSnippetNames)
                    {
                        CodeSnippetManager.DeleteByName(codeSnippetName);
                    }

                    foreach (string language in languages)
                    {
                        CodeSnippetManager.DeleteByLanguage(language);
                        LanguageManager.Delete(language);
                    }

                    this.ClearAll();
                    this.LanguageDataBinding();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source);
            }
        }

        private void saveBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                if (this.SaveCodeSnippet())
                {
                    this.LanguageDataBinding();
                    this.SetTreeViewImage();

                    this._isModified = false;
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source);
            }
        }

        private void pasteBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                this.codeMemoEdit.Paste();
            }
        }

        private void copyBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.codeMemoEdit.SelectionLength > 0)
            {
                this.codeMemoEdit.Copy();
            }
        }

        private void cutBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.codeMemoEdit.SelectionLength > 0)
            {
                this.codeMemoEdit.Cut();
            }
        }

        private void clearBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.ClearAll();
        }

        private void undoBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.codeMemoEdit.Undo();
        }

        private void selectAllBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.codeMemoEdit.SelectAll();
        }

        private void exitBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            bool allowExit = true;

            if (this.IsModified)
            {
                DialogResult result = XtraMessageBox.Show("Do you want to save changes before exiting?", "Notice", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                if (result == DialogResult.Yes)
                {
                    this.SaveCodeSnippet();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }

            if (allowExit)
            {
                Application.Exit();
            }
        }

        private void nameTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void functionTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void languageComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void languageComboBoxEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void summaryMemoEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void codeMemoEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }

        private void howDoIBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraMessageBox.Show("The help document will be delivered later.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }

        private void homePageBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Process.Start(global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.HomepageAddress);
        }

        private void feedbackBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Process.Start(global::Autumoon.CodeLibrary.Forms.Properties.Settings.Default.FeedbackLink);
        }

        private void purchaseBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            new PurchaseXtraForm().ShowDialog();
        }

        private void aboutBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            AboutXtraForm.Instance.ShowDialog();
        }

        private void themesRibbonGalleryBarItem_GalleryItemClick(object sender, GalleryItemClickEventArgs e)
        {
            this.SetTheme(e.Item.Caption);
        }

        private void directoryTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                foreach (TreeNode node in e.Node.Nodes)
                {
                    node.Checked = e.Node.Checked;
                }
            }

            bool nodeChecked = e.Node.Checked;

            this.SetDeleteButtonEnabled(nodeChecked);

            this.exportBarButtonItem.Enabled = nodeChecked;
        }

        private void codeMemoEdit_Click(object sender, EventArgs e)
        {
            this.SetEditButtonsEnabled(true);
        }

        private void codeMemoEdit_Leave(object sender, EventArgs e)
        {
            this.SetEditButtonsEnabled(false);
        }

        private void registerBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (OptionManager.CheckRegistered())
            {
                XtraMessageBox.Show("You have already registered.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                return;
            }

            if (XtraMessageBox.Show("Register function will collect your machine name, user name and IP address and send to the server and they will never be open, do you agree this?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                try
                {
                    OptionManager.SendRegister();

                    XtraMessageBox.Show("Register succeed, thank you!", "Succeed", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
                catch
                {
                    throw;
                }
            }
        }

        private void MainRibbonForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void exportBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.exportSaveFileDialog.ShowDialog();
        }

        private void exportSaveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string filePath = (sender as SaveFileDialog).FileName;
            Collection<string> codeSnippetNames = new Collection<string>();

            foreach (TreeNode node in this.directoryTreeView.Nodes)
            {
                if (node.Nodes == null && node.Nodes.Count == 0)
                {
                    continue;
                }

                foreach (TreeNode childNode in node.Nodes)
                {
                    if (childNode.Checked)
                    {
                        codeSnippetNames.Add(childNode.Text);
                    }
                }
            }

            try
            {
                CodeSnippetManager.ExportCodeSnippet(codeSnippetNames, filePath);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void importBarButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.importOpenFileDialog.ShowDialog();
        }

        private void importOpenFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            string importedFilePath = (sender as OpenFileDialog).FileName;

            try
            {
                int importedAmount = CodeSnippetManager.ImportCodeSnippet(importedFilePath);

                XtraMessageBox.Show((importedAmount + " code snippet" + (importedAmount > 1 ? "s were" : " was") + " imported succeed."), "Import Result", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                this.LanguageDataBinding();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        #endregion
    }
}